package ua.kiev.tosik.price.modules.clients.gui;

import org.apache.log4j.Logger;
import org.jdesktop.swingx.JXStatusBar;
import org.jdesktop.swingx.plaf.basic.BasicStatusBarUI;
import ua.kiev.tosik.price.modules.clients.conf.ModuleConfig;

import javax.swing.*;
import java.awt.*;

public class EditorStatusBar {

	private static Logger logger = Logger.getLogger("ua.kiev.tosik.price.modules.clients.gui");
	private EditorDialog editDialog;
	private JXStatusBar statusBar;
	private JLabel imageLabel;
	private JLabel statusLabel;
	private JProgressBar progressBar;
	private ImageIcon defaultStatusImage;
	private ImageIcon waitStatusImage;
	private ImageIcon warningStatusImage;
	private ModuleConfig config;

	public EditorStatusBar(EditorDialog editDialog) {
		logger.debug("Initialize Editor status bar");
		this.editDialog = editDialog;
		config = ModuleConfig.getInstance();
		statusBar = new JXStatusBar();
		statusBar.putClientProperty(BasicStatusBarUI.AUTO_ADD_SEPARATOR, true);
		JXStatusBar.Constraint c1 = new JXStatusBar.Constraint();
		c1.setFixedWidth(20);
		defaultStatusImage = new ImageIcon(config.get("clients.editor.statusbar.icon"));
		warningStatusImage = new ImageIcon(config.get("clients.editor.statusbar.warning.icon"));
		imageLabel = new JLabel();
		statusBar.add(imageLabel, c1);
		// statusBar.add(new JSeparator(JSeparator.VERTICAL));
		statusLabel = new JLabel();
		statusBar.add(statusLabel, JXStatusBar.Constraint.ResizeBehavior.FILL);
		progressBar = new JProgressBar(JProgressBar.HORIZONTAL, 0, 100);
		progressBar.setPreferredSize( new Dimension(200, 16) );
		progressBar.setValue(0);
		progressBar.setStringPainted(true);
		// statusBar.add(progressBar, new JXStatusBar.Constraint(200));
		waitStatusImage = new ImageIcon(config.get("clients.editor.statusbar.loading.icon"));
		reset();
	}

	public JXStatusBar getStatusBar() {
		return statusBar;
	}

	public void reset() {
		imageLabel.setIcon(defaultStatusImage);
		statusLabel.setText(config.get("clients.editor.statusbar.text"));
		showProgress(false);
		progressBar.setValue(0);
	}

	public void setWait() {
		setImageLabelIcon(waitStatusImage);
		setStatusLabelText(config.get("clients.editor.statusbar.loading.text"));
	}

	public void setDeleteWait() {
		setImageLabelIcon(waitStatusImage);
		setStatusLabelText(config.get("clients.editor.statusbar.deleting.text"));
	}

	public void setProgressMax(int value) {
		progressBar.setMaximum(value);
	}

	public int getProgressValue() {
		return progressBar.getValue();
	}

	public void setProgressValue(int value) {
		progressBar.setValue(value);
	}

	public void setProgressString(String text) {
		progressBar.setString(text);
	}

	public void showProgress(boolean show) {
		if ( show != progressBar.isVisible() ) {
			progressBar.setVisible(show);
		}
	}

	public void setWarning(String warningMessage) {
		setImageLabelIcon(warningStatusImage);
		setStatusLabelText(warningMessage);
	}

	private void setImageLabelIcon(ImageIcon icon) {
		imageLabel.setIcon(icon);
	}

	private void setStatusLabelText(String text) {
		statusLabel.setText(text);
	}

}